global class CalendarSorterNotFuture{
/**********************************************************************
 Name:  CalendarSorter
 Purpose:An interface for sort calendard                                        
======================================================
History                                                            
-------                                                            
VERSION  AUTHOR            DATE              DETAIL                                
   1.0         li siheng         2010-04-07       Create          
======================================================
Review History                                                            
-------  
VERSION    REVIEWER     DATE          DETAIL
  1.0         WP      05/06/2011    1. class的注释应该放到class体外，且class之上
                  2. 方法注释信息不足，应说明参数
                  3. 方法体内变量未做注释说明
                  4. 方法很长，缺少详细注释信息
                  5. testMethod做的各种Event只有一条，数据量不足
***********************************************************************/
  
    
    public static void Sort(set<date> sortDateList, set<id> owneridList ){  //target date + target event owner
        
        list<recordtype> rtl=new list<recordtype>();
        id NationalHCPRecordtype;
        id CallPlanRecordtype;
        id PocketSessionRecordtype;
        id PClassRecordtype;
        
        
        rtl=[select id,sobjecttype,developername from recordtype where
               (sobjecttype=:'event' and developername=:'nationalHCP')
               or (sobjecttype=:'event' and developername=:'CallPlan')
               or (sobjecttype=:'event' and developername=:'PocketSessionOrSaloon')
               or (sobjecttype=:'event' and developername=:'PClass')];
        for(recordtype r:rtl){                      //查出recordtypeid, 4种recordtype
            
            if(r.sobjecttype=='event' && r.developername=='CallPlan'){
                CallPlanRecordtype=r.id;
            }
            if(r.sobjecttype=='event' && r.developername=='PocketSessionOrSaloon'){
                PocketSessionRecordtype=r.id;
            }
            if(r.sobjecttype=='event' && r.developername=='PClass'){
                PClassRecordtype=r.id;
            }
            if(r.sobjecttype=='event' && r.developername=='nationalHCP'){
                NationalHCPRecordtype=r.id;
            }
        }
        //搜索出所有符合条件的event记录
        list<event> all=[select id,StartDateTime,whatid,recordtypeid,ownerid from event e where e.ownerid in:owneridList and e.eventdate__c in:sortDateList order by hospitalname__c limit 9500];
     //   system.assert(false,all);
        map<string,datetime> dateMap=new map<string,datetime>();//key is date+ownerid
        
        list<event> pocketl=new list<event>();
        list<event> pclassl=new list<event>();
        list<event> callplanl=new list<event>();
        for(event e:all){
            datemap.put(string.valueof(e.startdatetime.date())+e.ownerid,datetime.newInstancegmt(e.startdatetime.date().year(),e.startdatetime.date().month(),e.startdatetime.date().day(), 0, 0, 0));
            if(e.recordtypeid==PocketSessionRecordtype || e.recordtypeid==NationalHCPRecordtype){
                pocketl.add(e);
            }
            if(e.recordtypeid==PClassRecordtype){
                pclassl.add(e);
            }
            if(e.recordtypeid==CallPlanRecordtype){
                callplanl.add(e);
            }
        }
        list<event> insertl=new list<event>();
        for(event p:pocketl){
            datetime dtime=datemap.get(string.valueof(p.startdatetime.date())+p.ownerid);
            p.StartDateTime=dtime;
            dtime=dtime.addHours(1);
            datemap.put(string.valueof(p.startdatetime.date())+p.ownerid,dtime);
            p.EndDateTime=dtime;
            insertl.add(p);
        }
        for(event p:pclassl){
            datetime dtime=datemap.get(string.valueof(p.startdatetime.date())+p.ownerid);
            p.StartDateTime=dtime;
            dtime=dtime.addHours(1);
            datemap.put(string.valueof(p.startdatetime.date())+p.ownerid,dtime);
            p.EndDateTime=dtime;
            insertl.add(p);
        }
        for(event p:callplanl){
            datetime dtime=datemap.get(string.valueof(p.startdatetime.date())+p.ownerid);
            p.StartDateTime=dtime;
            dtime=dtime.addMinutes(30);
            datemap.put(string.valueof(p.startdatetime.date())+p.ownerid,dtime);
            p.EndDateTime=dtime;
            insertl.add(p);
        }
        /*
        list<event> updateevent=new list<event>();
        for(integer i=0;i<insertl.size();){
            updateevent.add(insertl[i]);
            while(updateevent.size()==100 || i==insertl.size()-1){
                update updateevent;
                updateevent.clear();
                break;
            }
            i++;
        }
        */
        update insertl;
            
       
        
    }
    
    static testMethod  void test()
    {
        Profile p = [select id from profile where Name='Regional General Manager' limit 1];
        User u2 = new User(alias = 'standtss', email='sdardr@testorgss.com',
        emailencodingkey='UTF-8', lastname='Testing', languagelocalekey='en_US',Title__c='TSR',
        localesidkey='en_US', profileid = p.Id,UserCity__c='广州',IsActive=true,
        UserAreaValue__c='广州区域',UserSubRegion__c='华南一区',UserRegion__c='华南区',
        timezonesidkey='America/Los_Angeles', username='stsers@testorg.com');
        insert u2;  
        
        list<recordtype> rtl=new list<recordtype>();
        id NationalHCPRecordtype;
        id CallPlanRecordtype;
        id PocketSessionRecordtype;
        id PClassRecordtype;
        
        
        rtl=[select id,sobjecttype,developername from recordtype where
               (sobjecttype=:'event' and developername=:'nationalHCP')
               or (sobjecttype=:'event' and developername=:'CallPlan')
               or (sobjecttype=:'event' and developername=:'PocketSessionOrSaloon')
               or (sobjecttype=:'event' and developername=:'PClass')];
        for(recordtype r:rtl){                      //查出recordtypeid, 4种recordtype
            
            if(r.sobjecttype=='event' && r.developername=='CallPlan'){
                CallPlanRecordtype=r.id;
            }
            if(r.sobjecttype=='event' && r.developername=='PocketSessionOrSaloon'){
                PocketSessionRecordtype=r.id;
            }
            if(r.sobjecttype=='event' && r.developername=='PClass'){
                PClassRecordtype=r.id;
            }
            if(r.sobjecttype=='event' && r.developername=='nationalHCP'){
                NationalHCPRecordtype=r.id;
            }
        }
        
        system.runas(u2){
             list<event> insertEvent=new list<event>();
             Event e = new Event(Subject='拜访', 
                                        //WhatId=,
                                        recordtypeid=CallPlanRecordtype,
                                        ownerid=userinfo.getuserid(),
                                        StartDateTime=datetime.now(),
                                        EndDateTime=datetime.now(),
                                        IsReminderSet = false,
                                        eventDate__c=date.today(),
                                        hospitalname__c='1'
                                         );
             insertEvent.add(e);
             
             Event e1 = new Event(Subject='HCP活动', 
                                        //WhatId=,
                                        recordtypeid=NationalHCPRecordtype,
                                        ownerid=userinfo.getuserid(),
                                        StartDateTime=datetime.now(),
                                        EndDateTime=datetime.now(),
                                        IsReminderSet = false,
                                        eventDate__c=date.today(),
                                        hospitalname__c='2'
                                         );
             insertEvent.add(e1);
             
             Event e2 = new Event(Subject='消费者活动', 
                                        //WhatId=,
                                        recordtypeid=PClassRecordtype,
                                        ownerid=userinfo.getuserid(),
                                        StartDateTime=datetime.now(),
                                        EndDateTime=datetime.now(),
                                        IsReminderSet = false,
                                        eventDate__c=date.today(),
                                        hospitalname__c='3'
                                         );
             insertEvent.add(e2);
             
             insert insertEvent;
             set<date> dateSet=new set<date>();
             set<id> ownerSet=new set<id>();
             dateset.add(date.today());
             ownerset.add(userinfo.getuserid());
             CalendarSorter.sort(dateset,ownerset);
        }
        
    }
    
}//end bracket of class